﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using Core.Library;

namespace Core.AdminUser
{
    public class AdminUserController
    {
        public static bool ValidateUser(string username, string password)
        {
            var status = AdminUserObject.IsSuperAdministrator + AdminUserObject.IsActived;
            return AdminUserDAL.AdminUser_Validate(username, password, status);
        }

        public static bool Login(string username, string password, bool rememberAccount)
        {
            Logout();
            if (ValidateUser(username, password))
            {
                FormsAuthentication.Initialize();
                var name = WebConfigurationManager.AppSettings["SitePrefix"] + username;
                var ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, DateTime.Now.AddMinutes(45), true, "", FormsAuthentication.FormsCookiePath);
                Trace.Write(FormsAuthentication.FormsCookiePath + FormsAuthentication.FormsCookieName);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                HttpContext.Current.Response.SetCookie(cookie);

                return true;
            }
            return false;
        }

        public static void Logout()
        {
            FormsAuthentication.SignOut();
            if (HttpContext.Current.Response.Cookies["name"] != null && HttpContext.Current.Response.Cookies["pass"] != null)
            {
                HttpContext.Current.Response.Cookies["name"].Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies["pass"].Expires = DateTime.Now.AddDays(-1);
            }
        }

        public static AdminUserObject AdminUser_GetById(int id)
        {
            return ObjectHelper.FillObject<AdminUserObject>(AdminUserDAL.AdminUser_GetById(id));
        }

        public static int AdminUser_Create(AdminUserObject o)
        {
            return AdminUserDAL.AdminUser_Create(o);
        }

        public static void AdminUser_Update(AdminUserObject o)
        {
            AdminUserDAL.AdminUser_Update(o);
        }

        public static void AdminUser_Delete(int userId)
        {
            AdminUserDAL.AdminUser_Delete(userId);
        }

        public static void AdminUser_UpdateInfomation(AdminUserObject o)
        {
            AdminUserDAL.AdminUser_UpdateInfomation(o);
        }

        public static void AdminUser_UpdatePassword(int userId, string pass)
        {
            AdminUserDAL.AdminUser_UpdatePassword(userId, pass);
        }

        public static AdminUserObject AdminUser_GetBy_Username_Status(string username, int status)
        {
            return ObjectHelper.FillObject<AdminUserObject>(AdminUserDAL.AdminUser_GetBy_Username_Status(username, status));
        }

        public static List<AdminUserObject> AdminUser_GetAllByStatuses(int statuses)
        {
            return ObjectHelper.FillCollection<AdminUserObject>(AdminUserDAL.AdminUser_GetAllByStatuses(statuses));
        }

        public static List<AdminUserObject> AdminUser_GetAllByStatusesByAdminGroupId(int adminGroupId, int statuses)
        {
            return ObjectHelper.FillCollection<AdminUserObject>(AdminUserDAL.AdminUser_GetAllByStatusesByAdminGroupId(adminGroupId, statuses));
        }        
    }
}